<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.projectId">
	          <j-search-select v-model:value="formData.projectId" dict="tm_project,project_name,id" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="系统名称" v-bind="validateInfos.systemId">
	          <j-search-select v-model:value="formData.systemId" dict="tm_project_system,system_name,id" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="众测批次" v-bind="validateInfos.batchId">
	          <j-search-select v-model:value="formData.batchId" dict="tm_crowd_test_plan where pid='0',plan_name,id" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="模块名称" v-bind="validateInfos.moduleName">
            <a-input v-model:value="formData.moduleName" placeholder="请输入模块名称" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="功能点" v-bind="validateInfos.functionName">
            <a-input v-model:value="formData.functionName" placeholder="请输入功能点" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷等级" v-bind="validateInfos.defectLevel">
	          <j-dict-select-tag v-model:value="formData.defectLevel" dictCode="defect_level" placeholder="请选择缺陷等级" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷类型" v-bind="validateInfos.defectType">
	          <j-dict-select-tag v-model:value="formData.defectType" dictCode="defect_type" placeholder="请选择缺陷类型" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷描述" v-bind="validateInfos.defectTitle">
	          <a-textarea v-model:value="formData.defectTitle" :rows="4" placeholder="请输入缺陷描述" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="操作步骤" v-bind="validateInfos.description">
	          <a-textarea v-model:value="formData.description" :rows="4" placeholder="请输入操作步骤" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="轮次名称" v-bind="validateInfos.testStage">
	          <j-search-select v-model:value="formData.testStage" dict="tm_test_stage,test_stage_name,id" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="环境" v-bind="validateInfos.envType">
	          <j-dict-select-tag v-model:value="formData.envType" dictCode="env_type" placeholder="请选择环境" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷优先级" v-bind="validateInfos.defectPriority">
	          <j-dict-select-tag v-model:value="formData.defectPriority" dictCode="defect_priority" placeholder="请选择缺陷优先级" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷提出人" v-bind="validateInfos.ownerId">
	          <j-search-select v-model:value="formData.ownerId" dict="sys_user,realname,username" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷状态" v-bind="validateInfos.bugStatus">
	          <j-dict-select-tag v-model:value="formData.bugStatus" dictCode="bug_status" placeholder="请选择缺陷状态" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷处理人" v-bind="validateInfos.solverId">
	          <j-search-select v-model:value="formData.solverId" dict="sys_user,realname,username" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否有影响" v-bind="validateInfos.isInfects">
	          <j-dict-select-tag v-model:value="formData.isInfects" dictCode="yn" placeholder="请选择是否有影响" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="影响范围" v-bind="validateInfos.infects">
	          <a-textarea v-model:value="formData.infects" :rows="4" placeholder="请输入影响范围" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="不解决原因" v-bind="validateInfos.noSloveReason">
	          <a-textarea v-model:value="formData.noSloveReason" :rows="4" placeholder="请输入不解决原因" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷原因" v-bind="validateInfos.defectReason">
	          <a-textarea v-model:value="formData.defectReason" :rows="4" placeholder="请输入缺陷原因" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="原因分类" v-bind="validateInfos.defectReasonCata">
	          <j-dict-select-tag v-model:value="formData.defectReasonCata" dictCode="defect_reason_cata" placeholder="请选择原因分类" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="缺陷图片" v-bind="validateInfos.defectImg">
	          <j-image-upload  v-model:value="formData.defectImg" :disabled="disabled" allowClear></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="备注" v-bind="validateInfos.commets">
	          <a-textarea v-model:value="formData.commets" :rows="4" placeholder="请输入备注" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CrowdTestDefect.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    projectId: '',   
    systemId: '',   
    batchId: '',   
    moduleName: '',   
    functionName: '',   
    defectLevel: '',   
    defectType: '',   
    defectTitle: '',   
    description: '',   
    testStage: '',   
    envType: '',   
    defectPriority: '',   
    ownerId: '',   
    bugStatus: '',   
    solverId: '',   
    isInfects: '',   
    infects: '',   
    noSloveReason: '',   
    defectReason: '',   
    defectReasonCata: '',   
    defectImg: '',   
    commets: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    projectId: [{ required: true, message: '请输入项目名称!'},],
    systemId: [{ required: true, message: '请输入系统名称!'},],
    batchId: [{ required: true, message: '请输入众测批次!'},],
    functionName: [{ required: true, message: '请输入功能点!'},],
    defectLevel: [{ required: true, message: '请输入缺陷等级!'},],
    defectType: [{ required: true, message: '请输入缺陷类型!'},],
    defectTitle: [{ required: true, message: '请输入缺陷描述!'},],
    description: [{ required: true, message: '请输入操作步骤!'},],
    testStage: [{ required: true, message: '请输入轮次名称!'},],
    envType: [{ required: true, message: '请输入环境!'},],
    defectPriority: [{ required: true, message: '请输入缺陷优先级!'},],
    bugStatus: [{ required: true, message: '请输入缺陷状态!'},],
    solverId: [{ required: true, message: '请输入缺陷处理人!'},],
    isInfects: [{ required: true, message: '请输入是否有影响!'},],
    infects: [{ required: true, message: '请输入影响范围!'},],
    noSloveReason: [{ required: true, message: '请输入不解决原因!'},],
    defectReason: [{ required: true, message: '请输入缺陷原因!'},],
    defectReasonCata: [{ required: true, message: '请输入原因分类!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
